import {
  getSetting, chooseAddress, openSetting, showModal, showToast
} from '../../utlis/asyncWx';
// ES7兼容插件
// import regeneratorRuntime from "../../lib/runtime/runtime";
Page({
  data: {
    address: {}, // 购物地址
    cart: [],//购物车列表
    totalPrice: 0,//总价
    totalNum: 0,//总件数
    payList: []
  },
  // 监听页面显示
  onShow: function () {
    // 获取地址
    let address = wx.getStorageSync('address')
    this.setData({
      address
    })
    // 更新购物车
    let cart = wx.getStorageSync('cart') || []
    // 调用
    this.setCart(cart)
  },
  // 创建订单
  handleCreateOrder() {
    const token = wx.getStorageSync('token')
    if (!token) {
      wx.navigateTo({
        url: '/pages/auth/auth',
        success: (result) => {
        },
        fail: () => { }
      });
    }
    // 有token ,创建请求头
    const header = { Authorization: token }
  },
  // 设置购物车状态: 总数 总价 勾选--公用事件
  setCart(cart) {
    // 总价,总数
    let totalPrice = 0, totalNum = 0
    let payList = []
    // 遍历
    cart.forEach(v => {
      if (v.check) {
        totalPrice += v.num * v.goods_price
        totalNum += v.num
        payList.push(v)
      }
    })
    console.log('payList', payList);
    wx.setStorageSync("cart", cart)
    // 提交数据
    this.setData({
      cart,
      totalPrice,
      totalNum,
      payList
    })
  }
});